<?php
class Generic_model extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}
	
	function insert($data, $table)
	{
		return $this->db->insert($table, $data);
	}
	
	function getLastId()
	{
		return $this->db->insert_id();
	}
	
	function update($data, $table, $pk)
	{
        $this->db->flush_cache();
		if (is_array($pk))
		{
			foreach ($pk as $ids)
			{
				$this->db->where($ids, $data[$ids]);
			}
		}
		else
		{
			$this->db->where($pk, $data[$pk]);
		}
		return $this->db->update($table, $data);
	}
	
	function delete($data, $table, $pk)
	{
        $this->db->flush_cache();
		$this->db->where($pk, $data[$pk]);
		return $this->db->delete($table);
	}
	
	function getById($data, $table, $pk)
	{
        $this->db->flush_cache();
		$this->db->where($pk,$data[$pk]);
		$query = $this->db->get($table);
		return $query->result();
	}
	
	function getByFilterPaging($objSearch=NULL, $table, $data)
	{
        $this->db->flush_cache();
		$this->db->start_cache();
		if (!is_null($objSearch))
		{
			foreach ($objSearch->rules as $item)
			{
				switch ($item->op)
				{
					case "eq":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." =", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." =", $item->data);
						}
						break;
					case "ne":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <>", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <>", $item->data);
						}
						break;
					case "lt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <", $item->data);
						}
						break;
					case "le":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <=", $item->data);
						}
						break;
					case "gt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >", $item->data);
						}
						break;
					case "ge":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >=", $item->data);
						}
						break;
					case "bw":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'after');
						}
						break;
					case "bn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'after');
						}
						break;
					case "in":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_in($item->field, $arr_values);
						}
						break;
					case "ni":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_not_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_not_in($item->field, $arr_values);
						}
						break;
					case "ew":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'before');
						}
						break;
					case "en":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'before');
						}
						break;
					case "cn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'both');
						}
						break;
					case "nc":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'both');
						}
						break;
				}
			}
		}
		$page = $data['page'];
		$limit = $data['rows'];
		$sidx = $data['sidx'];
		$sord = $data['sord'];
		$this->db->from($table);
		$count = $this->db->count_all_results();
		
		if( $count > 0 && $limit > 0) { 
              $total_pages = ceil($count/$limit); 
        } else { 
              $total_pages = 0; 
        } 
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit * $page - $limit;
		if($start <0) $start = 0;
		$this->db->order_by($sidx , $sord);
		
		$this->db->limit($limit, $start);

		$query = $this->db->get($table);
		
		$data_result['result'] = $query->result();
        $data_result['page'] = $page;
        $data_result['totalpages'] = $total_pages;
        $data_result['totalrecords']=$count;

        $this->db->flush_cache();
		return $data_result;
	}	
	
	function getByFilter($objSearch=NULL, $table)
	{
        $this->db->flush_cache();
		$this->db->start_cache();
		if (!is_null($objSearch))
		{
			foreach ($objSearch->rules as $item)
			{
				switch ($item->op)
				{
					case "eq":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." =", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." =", $item->data);
						}
						break;
					case "ne":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <>", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <>", $item->data);
						}
						break;
					case "lt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <", $item->data);
						}
						break;
					case "le":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <=", $item->data);
						}
						break;
					case "gt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >", $item->data);
						}
						break;
					case "ge":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >=", $item->data);
						}
						break;
					case "bw":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'after');
						}
						break;
					case "bn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'after');
						}
						break;
					case "in":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_in($item->field, $arr_values);
						}
						break;
					case "ni":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_not_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_not_in($item->field, $arr_values);
						}
						break;
					case "ew":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'before');
						}
						break;
					case "en":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'before');
						}
						break;
					case "cn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'both');
						}
						break;
					case "nc":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'both');
						}
						break;
					case "ob":
						$this->db->order_by($item->field, $item->data);
						break;
				}
			}
		}

		$query = $this->db->get($table);
		
		$result = $query->result();

        $this->db->flush_cache();

		return $result;
	}

	function deleteByFilter($objSearch=NULL, $table)
	{
		$this->db->start_cache();
		if (!is_null($objSearch))
		{
			foreach ($objSearch->rules as $item)
			{
				switch ($item->op)
				{
					case "eq":
						if ($item->groupOp == "AND")
						{
							$this->db->where($item->field." =", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." =", $item->data);
						}
						break;
					case "ne":
						if ($item->groupOp == "AND")
						{
							$this->db->where($item->field." <>", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <>", $item->data);
						}
						break;
					case "lt":
						if ($item->groupOp == "AND")
						{
							$this->db->where($item->field." <", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <", $item->data);
						}
						break;
					case "le":
						if ($item->groupOp == "AND")
						{
							$this->db->where($item->field." <=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <=", $item->data);
						}
						break;
					case "gt":
						if ($item->groupOp == "AND")
						{
							$this->db->where($item->field." >", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >", $item->data);
						}
						break;
					case "ge":
						if ($item->groupOp == "AND")
						{
							$this->db->where($item->field." >=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >=", $item->data);
						}
						break;
					case "bw":
						if ($item->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'after');
						}
						break;
					case "bn":
						if ($item->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'after');
						}
						break;
					case "in":
						$arr_values = explode(";", $item->data);
						if ($item->groupOp == "AND")
						{
							$this->db->where_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_in($item->field, $arr_values);
						}
						break;
					case "ni":
						$arr_values = explode(";", $item->data);
						if ($item->groupOp == "AND")
						{
							$this->db->where_not_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_not_in($item->field, $arr_values);
						}
						break;
					case "ew":
						if ($item->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'before');
						}
						break;
					case "en":
						if ($item->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'before');
						}
						break;
					case "cn":
						if ($item->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'both');
						}
						break;
					case "nc":
						if ($item->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'both');
						}
						break;
					case "ob":
						$this->db->order_by($item->field, $item->data);
						break;
				}
			}
		}

		$result = $this->db->delete($table);
        $this->db->flush_cache();
		return $result;
	}
	
	function getAll($table)
	{
		$query = $this->db->get($table);
		$result = $query->result();

		return $result;
	}
	
	function count($objSearch=NULL, $table)
	{
        $this->db->flush_cache();
		if (!is_null($objSearch))
		{
			foreach ($objSearch->rules as $item)
			{
				switch ($item->op)
				{
					case "eq":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." =", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." =", $item->data);
						}
						break;
					case "ne":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <>", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <>", $item->data);
						}
						break;
					case "lt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <", $item->data);
						}
						break;
					case "le":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." <=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." <=", $item->data);
						}
						break;
					case "gt":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >", $item->data);
						}
						break;
					case "ge":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where($item->field." >=", $item->data);
						}
						else
						{
							$this->db->or_where($item->field." >=", $item->data);
						}
						break;
					case "bw":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'after');
						}
						break;
					case "bn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'after');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'after');
						}
						break;
					case "in":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_in($item->field, $arr_values);
						}
						break;
					case "ni":
						$arr_values = explode(";", $item->data);
						if ($objSearch->groupOp == "AND")
						{
							$this->db->where_not_in($item->field, $arr_values);
						}
						else
						{
							$this->db->or_where_not_in($item->field, $arr_values);
						}
						break;
					case "ew":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'before');
						}
						break;
					case "en":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'before');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'before');
						}
						break;
					case "cn":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_like($item->field, $item->data,'both');
						}
						break;
					case "nc":
						if ($objSearch->groupOp == "AND")
						{
							$this->db->not_like($item->field, $item->data,'both');
						}
						else
						{
							$this->db->or_not_like($item->field, $item->data,'both');
						}
						break;
				}
			}
		}

		$this->db->from($table);
		
		$result = $this->db->count_all_results();

		return $result;
	}

}
?>